﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using YJKPatternSign.StairsRoomSection.DataDefine;
using YJKPatternSign.StairsRoomSection.ElementPacket;
using YJKLoopSearch;

namespace YJKPatternSign.StairsRoomSection.Kit.CutLine
{
	public class CutLineGetter
	{
		public virtual bool GetCutLines(SectionViewData ViewInfoRay, ElementObject elemObj)
		{
			Transform linkTran = elemObj.GetLinkTran();
			SectionViewData sectionViewData;
			this.TransformViewData(elemObj, ViewInfoRay, out sectionViewData);
			XYZ endPoint = sectionViewData.m_SecLine.GetEndPoint(0);
			XYZ endPoint2 = sectionViewData.m_SecLine.GetEndPoint(1);
			XYZ origin = (endPoint + endPoint2) / 2.0;
			XYZ viewDir = sectionViewData.m_viewDir;
			new List<Curve>();
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(viewDir, origin);
			List<Curve> collection = CommonFun.TranCurve(CommonFun.CutElement(elemObj.SelfElem.UseElement, sectionViewData, plane), linkTran);
			elemObj.m_lstCutLine.AddRange(collection);
			return true;
		}

		public void TransformViewData(ElementObject elemObj, SectionViewData ViewInfoRay, out SectionViewData ViewInfo)
		{
			CommonFun.TransformViewData(elemObj.SelfElem, ViewInfoRay, out ViewInfo);
		}

		public static List<List<Curve>> DivStairsCurves(List<Curve> curves, SectionViewData viewSectionInfo)
		{
			if (curves.Count == 0)
			{
				return null;
			}
			StructureLoopSearcher structureLoopSearcher = new StructureLoopSearcher(viewSectionInfo.SectionView.Document);
			Transform transform = Transform.Identity;
			Autodesk.Revit.DB.View sectionView = viewSectionInfo.SectionView;
			transform.Origin = sectionView.Origin;
			transform.BasisX = sectionView.RightDirection;
			transform.BasisY = sectionView.UpDirection;
			transform.BasisZ = transform.BasisX.CrossProduct(transform.BasisY);
			transform = transform.Inverse;
			List<Pole2D> list = new List<Pole2D>();
			foreach (Curve curve in curves)
			{
				Pole2D item = Pole2D.TransformWallToStructure2DPole(curve.CreateTransformed(transform), null, 0.0, 0.0);
				list.Add(item);
			}
			List<List<Curve>> cellsEdgeLoops = structureLoopSearcher.GetCellsEdgeLoops(list, true);
			List<List<Curve>> list2 = new List<List<Curve>>();
			foreach (List<Curve> list3 in cellsEdgeLoops)
			{
				List<Curve> list4 = new List<Curve>();
				foreach (Curve curve2 in list3)
				{
					Curve item2 = curve2.CreateTransformed(transform.Inverse);
					list4.Add(item2);
				}
				list2.Add(list4);
			}
			return list2;
		}
	}
}
